package com.niit.yhserver.service.impl;

import com.niit.yhpojo.dto.EmployeeDTO;
import com.niit.yhpojo.entity.Employee;
import com.niit.yhserver.mapper.EmployeeMapper;
import com.niit.yhserver.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> getAllEmployee() {
        return employeeMapper.getAllEmployee();
    }

    @Override
    public Employee findOneById(Integer id) {
        return employeeMapper.findOneById(id);
    }

    @Override
    public void insert(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();

        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO,employee);

        //设置当前记录的创建时间和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        employeeMapper.insert(employee);
    }

    @Override
    public void deleteById(Integer id) {
        employeeMapper.deleteById(id);
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);

        employee.setUpdateTime(LocalDateTime.now());
        employeeMapper.update(employee);
    }


}
